package com.lh0811.forge.pserv.uacs.server.api.impl;

import com.lh0811.forge.dependency.basic_code.BeanHelper;
import com.lh0811.forge.dependency.basic_web.exception.CommonException;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.pserv.uacs.api.api.UacsOpenApi;
import com.lh0811.forge.pserv.uacs.api.param.UserRealNameAuthenticationParam;
import com.lh0811.forge.pserv.uacs.api.vo.GetDepartmentInfoVo;
import com.lh0811.forge.pserv.uacs.api.vo.GetPlateUserVo;
import com.lh0811.forge.pserv.uacs.api.vo.GetTenantInfoVo;
import com.lh0811.forge.pserv.uacs.server.repository.dao.UacsPlateTenantInfoDao;
import com.lh0811.forge.pserv.uacs.server.repository.dao.UacsPlateUserDao;
import com.lh0811.forge.pserv.uacs.server.repository.dao.UacsTenantDepartmentDao;
import com.lh0811.forge.pserv.uacs.server.repository.entity.UacsPlateTenantInfo;
import com.lh0811.forge.pserv.uacs.server.repository.entity.UacsPlateUser;
import com.lh0811.forge.pserv.uacs.server.repository.entity.UacsTenantDepartment;
import com.lh0811.forge.pserv.uacs.server.service.CurrentUserService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

@Service
public class UacsOpenApiImpl implements UacsOpenApi {

    @Resource
    private UacsPlateTenantInfoDao uacsPlateTenantInfoDao;

    @Resource
    private UacsTenantDepartmentDao uacsTenantDepartmentDao;

    @Resource
    private CurrentUserService currentUserService;

    @Resource
    private UacsPlateUserDao uacsPlateUserDao;


    // 获取用户信息
    @Override
    public GetPlateUserVo getUserInfoById(Long userId) throws Exception {
        UacsPlateUser uacsPlateUser = uacsPlateUserDao.getById(userId);
        return BeanHelper.copyBean(uacsPlateUser, GetPlateUserVo.class);
    }


    // 根据证件号获取用户信息
    @Override
    public GetPlateUserVo getUserInfoByIdCardNo(String idCardNo) throws Exception {
        UacsPlateUser uacsPlateUser = uacsPlateUserDao.lambdaQuery().eq(UacsPlateUser::getIdCardNo, idCardNo).one();
        return BeanHelper.copyBean(uacsPlateUser, GetPlateUserVo.class);
    }


    /**
     * 获取租户信息
     */
    @Override
    public GetTenantInfoVo getTenantInfo(Long tenantId) throws Exception {
        UacsPlateTenantInfo tenantInfo = uacsPlateTenantInfoDao.getById(tenantId);
        if (ObjectUtils.isEmpty(tenantInfo)) {
            throw CommonException.create(ServerResponse.createByError("租户信息不存在"));
        }
        return BeanHelper.copyBean(tenantInfo, GetTenantInfoVo.class);
    }

    /**
     * 获取组织机构信息
     */
    @Override
    public GetDepartmentInfoVo getDepartmentInfo(Long departmentId) throws Exception {
        UacsTenantDepartment department = uacsTenantDepartmentDao.getById(departmentId);
        if (ObjectUtils.isEmpty(department)) {
            throw CommonException.create(ServerResponse.createByError("组织机构不存在"));
        }
        return BeanHelper.copyBean(department, GetDepartmentInfoVo.class);
    }

    @Override
    public void userRealNameAuthentication(UserRealNameAuthenticationParam param) throws Exception {
        currentUserService.userRealNameAuthentication(param);
    }


    @Override
    public void saveUserPhoneForCurrentUser(String phone) throws Exception {
        currentUserService.userBuildingPhoneForCurrentUser(phone);
    }


}
